package me.firstapp.service.system.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.module.system.ApiClient;
import me.firstapp.module.system.ApiClientRecord;
import me.firstapp.repository.system.ApiClientRecordRepository;
import me.firstapp.repository.system.ApiClientRepository;
import me.firstapp.service.system.ApiClientRecordService;

@Service
public class ApiClientRecordServiceImpl implements ApiClientRecordService {

	@Autowired
	private ApiClientRepository apiClientRepository;

	@Autowired
	private ApiClientRecordRepository apiClientRecordRepository;

	@Override
	public ApiClientRecord addApiClientRecord(Long apiClientId, Long dateTime, Long counts) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		if (dateTime == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "日期");
		}
		if (counts == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "数量");
		}
		ApiClient apiClient = (ApiClient) apiClientRepository.getById(apiClientId);
		if (apiClient == null) {
			throw new ServiceException(StatusHouse.API_CLIENT_NOT_EXIST);
		}
		ApiClientRecord record = new ApiClientRecord();
		record.setApiClientId(apiClientId);
		record.setCounts(counts);
		Date date = DateUtils.getDateFromTimeStamp(String.valueOf(dateTime), "yyyy-MM-dd");
		record.setDate(date);
		apiClientRecordRepository.save(record);
		return record;
	}

	@Override
	public Page<ApiClientRecord> findApiClientRecordPage(Long apiClientId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType, Date startTime, Date endTime) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		return apiClientRecordRepository.findApiClientRecordPage(apiClientId, pageNo, pageSize, searchParams, sortParam,
				sortType, startTime, endTime);
	}

	@Override
	public Long findApiClientRecordTotalTimes(Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		Long totalTimes = apiClientRecordRepository.findApiClientRecordTotalTimes(apiClientId);
		return totalTimes;
	}

	@Override
	public Long findApiClientRecordMeanTimes(Long apiClientId) {
		if (apiClientId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "apiClientId");
		}
		Long totalTimes = apiClientRecordRepository.findApiClientRecordTotalTimes(apiClientId);
		Long counts = apiClientRecordRepository.findApiClientRecordCounts(apiClientId);
		Long meanTimes = 0L;
		if (counts != 0) {
			meanTimes = totalTimes / counts;
		}
		return meanTimes;
	}

}
